﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Threading;

namespace SmartSpace.Silverlight
{
    public class MessageService
    {
        private static MessageService s_instance;

        private static MessageService Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new MessageService();
                }
                return s_instance;
            }
        }

        private Popup m_popup;
        private PopupMessage m_popupMessage;
        private SlViewContainer m_target;

        private MessageService()
        {
            m_popup = new Popup();

            m_popupMessage = new PopupMessage();


            m_popupMessage.myStoryboard.Completed += delegate
            {
                m_popup.IsOpen = false;
            };

            m_popup.Child = m_popupMessage;

            m_popup.LayoutUpdated += new EventHandler(m_popup_LayoutUpdated);
            m_popup.IsOpen = true;
        }

        void m_popup_LayoutUpdated(object sender, EventArgs e)
        {
            //Instance.m_popup.Margin = new Thickness((Application.Current.Host.Content.ActualWidth - Instance.m_popupMessage.ActualWidth) / 2, (Application.Current.Host.Content.ActualHeight - Instance.m_popupMessage.ActualHeight) / 2, 0, 0);

            Instance.m_popup.Margin = new Thickness((Application.Current.Host.Content.ActualWidth - Instance.m_popupMessage.ActualWidth) / 2,0, 0, 0);
        }

        public static void ShowMessage(string content)
        {
            ShowMessage(MessageType.Information, content);

        }

        public static void ShowMessage(MessageType type, string content)
        {

            Instance.m_popupMessage.SetMessage(type, content);

            Instance.Show();
        }

        //public static void HideMessage()
        //{
        //    Instance.Hide();
        //}

        static void Content_Resized(object sender, EventArgs e)
        {
            //Instance.m_popup.Margin = new Thickness((Application.Current.Host.Content.ActualHeight - Instance.m_popupMessage.ActualHeight) / 2, (Application.Current.Host.Content.ActualHeight - Instance.m_popupMessage.ActualHeight) / 2, 0, 0);
        }

        private void Show()
        {
            //Instance.m_popup.Margin = new Thickness((Application.Current.Host.Content.ActualHeight - Instance.m_popupMessage.ActualHeight) / 2, (Application.Current.Host.Content.ActualHeight - Instance.m_popupMessage.ActualHeight) / 2, 0, 0);

            Instance.m_popup.IsOpen = true;
            m_popupMessage.myStoryboard.Begin();
        }

        //private void Hide()
        //{
        //    //Instance.m_popup.IsOpen = false;

        //    m_popupMessage.myStoryboard.Begin();
        //}
    }

    public enum MessageType
    {
        Information = 1,
        Warrning = 2,
        Error=3,
    }
}
